package com.smile.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.smile.api.FlyApi;
import com.smile.api.OperateLogApi;
import com.smile.api.PayloadApi;
import com.smile.commons.action.ActionUtils;
import com.smile.commons.page.R;
import com.smile.mapper.*;
import com.smile.model.domain.*;
import com.smile.model.dto.request.FlyRequestDTO;
import com.smile.model.dto.request.FlyStickControlRequestDTO;
import com.smile.model.dto.request.payload.CameraFocalLengthSetRequestDTO;
import com.smile.model.dto.request.payload.GimbalResetRequestDTO;
import com.smile.model.dto.request.payload.PayloadAuthorityGrabRequestDTO;
import com.smile.model.request.*;
import com.smile.model.response.response.GetFlyResultResponseDTO;
import com.smile.model.response.response.PayloadDataResponseDTO;
import com.smile.request.AddOperateLogRequest;
import com.smile.service.FlyService;
import com.smile.service.PayloadService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.math.RoundingMode;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.UUID;
import java.util.concurrent.locks.ReentrantLock;

@Service
@Slf4j
public class PayloadServiceImpl implements PayloadService {


    @Autowired
    private AirportMapper airportMapper;

    //流媒体推流地址
    @Value("${zlm.url}")
    private String zlmUrl;
    @Autowired
    private FlightTaskMapper flightTaskMapper;

    @Autowired
    private AirportInfoMapper  airportInfoMapper;
    @Autowired
    private OperateLogApi operateLogApi;
    @Autowired
    private DroneMapper droneMapper;

    @Autowired
    private DronePayloadMapper dronePayloadMapper;

    @Autowired
    private AirportDroneMapper airportDroneMapper;

    @Autowired
    private PayloadApi payloadApi;

    private ReentrantLock reentrantLock = new ReentrantLock();
    @Autowired
    FlyApi flyApi;

    @Override
    public GetFlyResultResponseDTO payloadAuthorityGrab(PayloadAuthorityGrabRequestDTO flyRequestDTO) throws IOException{
        GetFlyResultResponseDTO getFlyResultResponseDTO = new  GetFlyResultResponseDTO();
        //查询出相关配置
        getFlyResultResponseDTO.setCode(200);
        getFlyResultResponseDTO.setStatus(false);
        FeisuanAirport feisuanAirport = airportMapper.selectById(flyRequestDTO.getId());
        if(!ObjectUtil.isNull(feisuanAirport)){
            QueryWrapper<FeisuanAirportDrone> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("airport_id",feisuanAirport.getId());
            FeisuanAirportDrone feisuanAirportDrone  = airportDroneMapper.selectOne(queryWrapper);
            if(ObjectUtil.isNotNull(feisuanAirportDrone)){
                FeisuanDrone feisuanDrone = droneMapper.selectById(feisuanAirportDrone.getDroneId());
                if(ObjectUtil.isNotNull(feisuanDrone)){
                    QueryWrapper<FeisuanDronePayload> queryWrapper1 = new QueryWrapper<>();
                    queryWrapper1.eq("drone_sn",feisuanDrone.getDroneSn());
                    FeisuanDronePayload feisuanDronePayload = dronePayloadMapper.selectOne(queryWrapper1);
                    if(ObjectUtil.isNotNull(feisuanDronePayload)){
                        PayloadAuthorityGrabRequest payloadAuthorityGrabRequest = new PayloadAuthorityGrabRequest();
                        try {
                            payloadAuthorityGrabRequest.setPayloadIndex(feisuanDronePayload.getPayloadIndex());
                            payloadAuthorityGrabRequest.setGateway(feisuanAirport.getGateway());
                            payloadApi.payloadAuthorityGrab(payloadAuthorityGrabRequest);
                            getFlyResultResponseDTO.setStatus(true);
                        }catch (Exception e){

                        }
                    }
                }
            }
        }
        return   getFlyResultResponseDTO;
    }

    @Override
    public GetFlyResultResponseDTO cameraFocalLengthSet(CameraFocalLengthSetRequestDTO flyRequestDTO) throws IOException{
        GetFlyResultResponseDTO getFlyResultResponseDTO = new  GetFlyResultResponseDTO();
        //查询出相关配置
        getFlyResultResponseDTO.setCode(200);
        getFlyResultResponseDTO.setStatus(false);
        QueryWrapper<FeisuanAirport>  queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.eq("gateway_sn",flyRequestDTO.getGateway());
        queryWrapper3.last("limit 1");
        FeisuanAirport feisuanAirport = airportMapper.selectOne(queryWrapper3);

        if(!ObjectUtil.isNull(feisuanAirport)){
            QueryWrapper<FeisuanAirportDrone> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("airport_id",feisuanAirport.getId());
            FeisuanAirportDrone feisuanAirportDrone  = airportDroneMapper.selectOne(queryWrapper);
            if(ObjectUtil.isNotNull(feisuanAirportDrone)){
                FeisuanDrone feisuanDrone = droneMapper.selectById(feisuanAirportDrone.getDroneId());
                if(ObjectUtil.isNotNull(feisuanDrone)){
                    QueryWrapper<FeisuanDronePayload> queryWrapper1 = new QueryWrapper<>();
                    queryWrapper1.eq("drone_sn",feisuanDrone.getDroneSn());
                    FeisuanDronePayload feisuanDronePayload = dronePayloadMapper.selectOne(queryWrapper1);
                    if(ObjectUtil.isNotNull(feisuanDronePayload)){
                        CameraFocalLengthSetRequest cameraFocalLengthSetRequest = new CameraFocalLengthSetRequest();
                        try {
                            cameraFocalLengthSetRequest.setPayloadIndex(feisuanDronePayload.getPayloadIndex());
                            cameraFocalLengthSetRequest.setGateway(feisuanAirport.getGateway());
                            cameraFocalLengthSetRequest.setCameraType(flyRequestDTO.getCameraType());
                            cameraFocalLengthSetRequest.setZoomFactor(flyRequestDTO.getZoomFactor());
                            payloadApi.cameraFocalLengthSet(cameraFocalLengthSetRequest);
                            getFlyResultResponseDTO.setStatus(true);
                        }catch (Exception e){

                        }
                    }
                }
            }
        }
        return   getFlyResultResponseDTO;
    }

    @Override
    public String getPayloadIndex(String gatewaySn) throws IOException{
        String payloadIndex = null;
        QueryWrapper<FeisuanAirport>  queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.eq("gateway_sn",gatewaySn);
        queryWrapper3.last("limit 1");
        FeisuanAirport feisuanAirport = airportMapper.selectOne(queryWrapper3);

        if(!ObjectUtil.isNull(feisuanAirport)){
            QueryWrapper<FeisuanAirportDrone> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("airport_id",feisuanAirport.getId());
            FeisuanAirportDrone feisuanAirportDrone  = airportDroneMapper.selectOne(queryWrapper);
            if(ObjectUtil.isNotNull(feisuanAirportDrone)){
                FeisuanDrone feisuanDrone = droneMapper.selectById(feisuanAirportDrone.getDroneId());
                if(ObjectUtil.isNotNull(feisuanDrone)){
                    QueryWrapper<FeisuanDronePayload> queryWrapper1 = new QueryWrapper<>();
                    queryWrapper1.eq("drone_sn",feisuanDrone.getDroneSn());
                    FeisuanDronePayload feisuanDronePayload = dronePayloadMapper.selectOne(queryWrapper1);
                    if(ObjectUtil.isNotNull(feisuanDronePayload)){
                        CameraFocalLengthSetRequest cameraFocalLengthSetRequest = new CameraFocalLengthSetRequest();
                        try {
                            payloadIndex = feisuanDronePayload.getPayloadIndex();
                        }catch (Exception e){

                        }
                    }
                }
            }
        }
        return   payloadIndex;
    }


    @Override
    public PayloadDataResponseDTO getPayloadData(String gatewaySn) throws IOException{
        String payloadIndex = null;
        PayloadDataResponseDTO  payloadDataResponseDTO = new  PayloadDataResponseDTO();
        QueryWrapper<FeisuanAirport>  queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.eq("gateway_sn",gatewaySn);
        queryWrapper3.last("limit 1");
        FeisuanAirport feisuanAirport = airportMapper.selectOne(queryWrapper3);

        if(!ObjectUtil.isNull(feisuanAirport)){
            QueryWrapper<FeisuanAirportDrone> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("airport_id",feisuanAirport.getId());
            FeisuanAirportDrone feisuanAirportDrone  = airportDroneMapper.selectOne(queryWrapper);
            if(ObjectUtil.isNotNull(feisuanAirportDrone)){
                FeisuanDrone feisuanDrone = droneMapper.selectById(feisuanAirportDrone.getDroneId());
                if(ObjectUtil.isNotNull(feisuanDrone)){
                    QueryWrapper<FeisuanDronePayload> queryWrapper1 = new QueryWrapper<>();
                    queryWrapper1.eq("drone_sn",feisuanDrone.getDroneSn());
                    FeisuanDronePayload feisuanDronePayload = dronePayloadMapper.selectOne(queryWrapper1);
                    if(ObjectUtil.isNotNull(feisuanDronePayload)){
                        CameraFocalLengthSetRequest cameraFocalLengthSetRequest = new CameraFocalLengthSetRequest();
                        try {
                            payloadIndex = feisuanDronePayload.getPayloadIndex();
                            payloadDataResponseDTO.setSn(feisuanDrone.getDroneSn());
                            payloadDataResponseDTO.setPayloadIndex(payloadIndex);
                            payloadDataResponseDTO.setGateway(feisuanAirport.getGatewaySn());
                        }catch (Exception e){

                        }
                    }
                }
            }
        }
        return   payloadDataResponseDTO;
    }


    @Override
    public GetFlyResultResponseDTO gimbalReset(GimbalResetRequestDTO flyRequestDTO) throws IOException{
        GetFlyResultResponseDTO getFlyResultResponseDTO = new  GetFlyResultResponseDTO();
        //查询出相关配置
        getFlyResultResponseDTO.setCode(200);
        getFlyResultResponseDTO.setStatus(false);
        QueryWrapper<FeisuanAirport>  queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.eq("gateway_sn",flyRequestDTO.getGateway());
        queryWrapper3.last("limit 1");
        FeisuanAirport feisuanAirport = airportMapper.selectOne(queryWrapper3);
        if(!ObjectUtil.isNull(feisuanAirport)){
            QueryWrapper<FeisuanAirportDrone> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("airport_id",feisuanAirport.getId());
            FeisuanAirportDrone feisuanAirportDrone  = airportDroneMapper.selectOne(queryWrapper);
            System.out.println(feisuanAirportDrone);
            if(ObjectUtil.isNotNull(feisuanAirportDrone)){
                FeisuanDrone feisuanDrone = droneMapper.selectById(feisuanAirportDrone.getDroneId());
                System.out.println(feisuanDrone);
                if(ObjectUtil.isNotNull(feisuanDrone)){
                    QueryWrapper<FeisuanDronePayload> queryWrapper1 = new QueryWrapper<>();
                    queryWrapper1.eq("drone_sn",feisuanDrone.getDroneSn());
                    System.out.println(feisuanDrone.getDroneSn());
                    FeisuanDronePayload feisuanDronePayload = dronePayloadMapper.selectOne(queryWrapper1);
                    System.out.println(queryWrapper1);
                    if(ObjectUtil.isNotNull(feisuanDronePayload)){
                        GimbalResetRequest gimbalResetRequest = new GimbalResetRequest();
                        try {
                            gimbalResetRequest.setPayloadIndex(feisuanDronePayload.getPayloadIndex());
                            gimbalResetRequest.setGateway(feisuanAirport.getGateway());
                            gimbalResetRequest.setResetMode(flyRequestDTO.getResetMode());
                            System.out.println(gimbalResetRequest);
                            R r= payloadApi.gimbalReset(gimbalResetRequest);
                            System.out.println("执行结果");
                            System.out.println(r);
                            getFlyResultResponseDTO.setStatus(true);
                        }catch (Exception e){

                        }
                    }
                }
            }
        }
        return   getFlyResultResponseDTO;
    }


}
